var httpsUrl = getApp().globalData.httpsUrl;
var globalData = getApp().globalData;
var commonUtil = require("../../../utils/commonUtil.js")
Page({

  data: {
    imgUrl: globalData.imgUrl,
    zsimgs:[],//上传时展示图片
    groupId:'',//圈子id
    imgs: '',//多个图片用#号分隔开
  },

  save: function (e) {
    var frm = e.detail.value;//form内容
    frm.groupId = this.data.groupId;
    let imgs = this.data.imgs;
    if(imgs.length > 0){
      imgs = imgs.substring(0, imgs.length-1);
    }
    frm.imgs = imgs;
    console.log("参数：" + JSON.stringify(frm));
    let openId = wx.getStorageSync("openidKey");
    wx.showLoading({ title: '请稍等...' })
    wx.request({
      header: { "openId": openId, },
      url: httpsUrl + "/community/addArticle",
      dataType: "json",
      method: "POST",
      data: frm,
      success: function (res) {
        wx.hideLoading();
        res = res.data;
        console.log("返回：" + JSON.stringify(res));
        if (res.code == 0) {
          wx.navigateBack({
            delta: 1
          })
        } else {
          commonUtil.showModal(res.message);
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let info = wx.getStorageSync("qzKey");
    this.setData({ groupId: info.id });
  },
  addImg: function () {
    var that = this;
    let openId = wx.getStorageSync("openidKey");
    wx.chooseImage({
      count: 3,  //最多可以选择的图片总数
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        console.log("上传1："+JSON.stringify(res));
        var tempFilePaths = res.tempFilePaths;
        let zsimgs = that.data.zsimgs;
        zsimgs = zsimgs.concat(tempFilePaths);
        that.setData({ zsimgs: zsimgs })
        //启动上传等待中...
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 5000
        })
        var uploadImgCount = 0;
        for (var i = 0, h = tempFilePaths.length; i < h; i++) {
          console.log("tempFilePaths[i]:" + tempFilePaths[i]);
          wx.uploadFile({
            header: { "openId": openId, "Content-Type": "multipart/form-data" },
            url: httpsUrl + '/common/fileUpload',
            filePath: tempFilePaths[i],
            name: 'file',
            formData: {},
            success: function (res) {
              uploadImgCount++;
              console.log("res:" + JSON.stringify(res));
              let data = res.data;
              data = JSON.parse(data);
              console.log(data);
              if (data.code == 0) {
                var imgRetuenUrl = data.data;
                console.log("imgurl: " + imgRetuenUrl);
                let imgs = that.data.imgs;
                imgs = imgs + imgRetuenUrl+"#";
                that.setData({ imgs: imgs})
              } else {
                commonUtil.showModal(data.msg);
              }
              //如果是最后一张,则隐藏等待中
              if (uploadImgCount == tempFilePaths.length) {
                wx.hideToast();
              }
            },
            fail: function (res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false,
                success: function (res) { }
              })
            }
          });
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})